//导入api方法
import {  shopcartlist , add_shopcartlist,clear_shopcartlist,sub_shopcartlist} from '../../utils/api'
//需要用到vue

import vue from 'vue'

export default {
    namespaced:true,
    state:{
        shopcartlist:[],
        shopGoods:[],
        filter_shopcartlist:[]
    },
    mutations:{
        //购物车相关

        set_filter_shopcartlist(state,params){state.filter_shopcartlist = params},

        set_shopGoods(state,params){state.shopGoods=params},//某店铺中的所有商品
        set_shopcartlist(state,params){state.shopcartlist = params},
        //向shopcartlist中添加一个商品
        addgood(state,params){ 
             //如果 shopcartlist中没有该商品则push,且添加新的响应数据count:1，否则count+1
             
            let index =state.shopcartlist.findIndex((item)=>{
              
                return item.id == params.id
            })
          
            if(index != -1){
                state.shopcartlist[index].count+=1
            }else{
                params = JSON.parse( JSON.stringify(params) );
                vue.set(params,'count',1)
                state.shopcartlist.push( params)
            }
        },
        delet_shopcartlist(state){ state.shopcartlist=[] },//清空购物车

        reduce_shopcartlist(state,params){
           let index = state.shopcartlist.findIndex((item)=>{
                return item.id==params.id
            })
            if(state.shopcartlist[index].count<=1){
                state.shopcartlist.splice(index,1)
            }else{
                state.shopcartlist[index].count--
            }
        }
    },
    actions:{
       
          quest_shopcartlist({commit}){
                //先获取用户信息
               let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info

               shopcartlist({userid:userinfo.id}).then((res)=>{
                    if(res.data.code==200){
                         commit('set_shopcartlist',res.data.list)
                    }
               })
          },

          quest_addgood({commit},params){
               //先获取用户信息
               let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info
               add_shopcartlist({userid:userinfo.id,goodid:params.id}).then((res)=>{
                    if(res.data.code==200){
                        commit('addgood',params)
                    }
               })
          },

          quest_delet_shopcartlist({commit}){
            //先获取用户信息
            let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info
            clear_shopcartlist({userid:userinfo.id}).then((res)=>{
                if(res.data.code==200){
                    commit('delet_shopcartlist')
                }
            })
          },

          quset_reduce_shopcartlist({commit},params){
                 //先获取用户信息
               let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info
               sub_shopcartlist({userid:userinfo.id,goodid:params.id}).then((res)=>{
                    if(res.data.code==200){
                        commit('reduce_shopcartlist',params)
                    }
               })
          }

    },
    getters:{
        totalcount(state){
            return state.filter_shopcartlist.reduce((t,item)=>{
                return t+item.count
            },0)
        },
        totalPrice(state){
            return state.filter_shopcartlist.reduce((t,item)=>{
                return t+(item.count*item.min_price)
            },0).toFixed(2)
        }
    }

}